<template>
  <view class="settle-container">
    <!-- 全选 -->
    <label class="radio" @click="fullAllClick">
      <radio color="#C00000" :checked="isSelectedAll" /><text>全选</text>
    </label>

    <!-- 合计 -->
    <view class="amount-box">
      合计：<text>￥{{checkedGoodsAmount}}</text>
    </view>
    <!-- 结算按钮 -->
    <view class="settle-box">
      <button @click="amountSettle">结算({{checkedCount}})</button>
    </view>
  </view>
</template>

<script>
  import { mapState, mapGetters, mapMutations } from 'vuex'
  export default {
    computed: {
      ...mapGetters('m_cart', ['checkedCount', 'total', 'checkedGoodsAmount']),
      ...mapGetters('user', ['addstr']),
      ...mapState('user', ['token']),
      ...mapState('m_cart', ['cart']),
      isSelectedAll() {
        // 全选的条件 就是判断 所选商品的数量是否跟购物车总数量相同
        return this.total === this.checkedCount
      }
    },
    data() {
      return {
        seconds: 3,
        timer: null
      };
    },
    methods: {
      ...mapMutations('m_cart', ['updateAllGoodsState']),
      ...mapMutations('user', ['updateRedirectInfo']),
      fullAllClick() {
        this.updateAllGoodsState(!this.isSelectedAll)
      },
      amountSettle() {
        if(!this.checkedCount) return uni.$showMsg('请选择要结算的商品！')
        if(!this.addstr) return uni.$showMsg('请选择收货地址！')
        if(!this.token) return this.countDownTONavigate()
        this.payOrder()
      },
      // 创建订单
      async payOrder() {
        // 1.创建订单
        const orderInfo = {
            // 开发期间，注释掉真实的订单价格，
            // order_price: this.checkedGoodsAmount,
            // 写死订单总价为 1 分钱
            order_price: 0.01,
            consignee_addr: this.addstr,
            goods: this.cart.filter(x => x.goods_state).map(x => ({ goods_id: x.goods_id, goods_number: x.goods_count, goods_price: x.goods_price }))
          }
        const {data: res} = await uni.$http.post('/api/public/v1/my/orders/create', orderInfo)
        if(res.meta.status !== 200) return uni.$showMsg('创建订单失败！')
        const orderNumber = res.message.order_number
        // 2.订单与支付
        const { data: res2 } = await uni.$http.post('/api/public/v1/my/orders/req_unifiedorder', { order_number: orderNumber })
        if(res2.meta.status !== 200) return uni.$showMsg('预付订单生成失效！')
        const payInfo = res2.message.pay
        // 3.发起微信支付
        const [err, succ] = await uni.requestPayment(payInfo).catch(err => err)
        if(err) return uni.$showMsg('订单未支付！')
        const { data: res3 } = await uni.$http.post('/api/public/v1/my/orders/chkOrder', { order_number: orderNumber })
        if(res3.meta.status !== 200) return uni.$showMsg('订单未支付成功！')
        uni.showToast({
          title: '支付成功！',
          icon: 'success'
        })
        
      },
      // 提示倒计时
      showToast(n) {
        uni.showToast({
          title: '请登录后再结算！' + n + '秒后自动跳转到登录页',
          icon: 'none',
          mask: true,
          duration: 1500
        })
      },
      // 未登录倒计时跳转
      countDownTONavigate() {
        this.seconds = 3
        this.showToast(this.seconds)
        this.timer = setInterval(() => {
          if(this.seconds === 0) {
            clearInterval(this.timer)
            uni.switchTab({
              url: '/pages/my/my',
              success: () => {
                this.updateRedirectInfo({
                  // 跳转方式
                  openType: 'switchTab',
                  // 从哪个页面跳过去的
                  from: '/page/cart/cart'
                })
              }
            })
            return
          }
          this.seconds--
          this.showToast(this.seconds)
        }, 1000) 
      }
    }
  }
</script>

<style lang="scss">
  .settle-container {
    width: 100%;
    height: 100rpx;
    position: fixed;
    left: 0;
    bottom: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding-left: 5px;
    font-size: 28rpx;
    background-color: #FFFFFF;

    .radio {
      display: flex;
      align-items: center;
      text {}
    }

    .amount-box {
      text {
        color: #C00000;
        font-weight: bold;
      }
    }

    .settle-box {
      button {
        height: 100rpx;
        background-color: #C00000;
        border-radius: 0;
        color: #FFFFFF;
        min-width: 200rpx;
        &:active {
          background-color: #ad0000;
        }
      }
    }
  }
</style>
